package com.zxt.content.user.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wf.captcha.ArithmeticCaptcha;
import com.zxt.content.common.Bean.Result;
import com.zxt.content.common.utils.ResultCode;
import com.zxt.content.user.req.LoginBodyDTO;
import com.zxt.content.user.service.BUserService;
import com.zxt.content.user.utils.UserRedisKey;
import com.zxt.content.user.utils.RedisUtils;
import com.zxt.content.user.valid.LoginGroup;
import com.zxt.content.user.valid.RegisterGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author zxt
 */
@Slf4j
@RequestMapping("/user/login")
@RestController
@AllArgsConstructor
@Api(tags = "登录管理接口")
@CrossOrigin
public class LoginController {

    private final BUserService bUserService;

    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping("/code")
    public Result code() {
        Map<String, String> data = new HashMap<>();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
        // 获取运算的公式
        captcha.getArithmeticString();
        // 获取运算的结果
        String text = captcha.text();
        data.put("uuid", uuid);
        data.put("img", captcha.toBase64());
        // 验证码结果放到缓存中，30分钟有效
        RedisUtils.set(UserRedisKey.getCodeKey(uuid), text, TimeUnit.MINUTES, 30);
        return Result.newSuccess(data);
    }

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody @Validated(value = RegisterGroup.class) LoginBodyDTO dto) {
        // 验证码
        if (StringUtils.isBlank(dto.getUuid()) || StringUtils.isBlank(dto.getCode())) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        String codeKey = UserRedisKey.getCodeKey(dto.getUuid());
        Object codeObject = RedisUtils.get(codeKey);
        if (codeObject == null) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        if (!dto.getCode().equals(codeObject.toString())) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        RedisUtils.delete(codeKey);
        // 生成令牌
        bUserService.register(dto);
        return Result.newSuccess();
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody @Validated(value = LoginGroup.class) LoginBodyDTO dto) {
        // 验证码
        /*if (StringUtils.isBlank(dto.getUuid()) || StringUtils.isBlank(dto.getCode())) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        String codeKey = UserRedisKey.getCodeKey(dto.getUuid());
        Object codeObject = RedisUtils.get(codeKey);
        if (codeObject == null) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        if (!dto.getCode().equals(codeObject.toString())) {
            return Result.newError(ResultCode.CAPTCHA_ERROR);
        }
        RedisUtils.delete(codeKey);*/
        // 生成令牌
        String token = bUserService.login(dto);
        return Result.newSuccess(token);
    }

}
